package org.fei.ClassDiagram.data;

import java.util.List;
import org.netbeans.modules.classfile.ClassFile;
import org.netbeans.modules.classfile.Method;
import org.netbeans.modules.classfile.Parameter;
import org.fei.ClassDiagram.WindowComponents.Message;
/**
 *
 * @author Tomas
 */
public class MethodData extends Data {

    protected Method met;
    private boolean isConstructor;
    public MethodData(Method method, ClassFile classFile) {
        met = method;
        //Message.showMessage("metoda " + met.getName() + " " + met.getTypeSignature());
        isConstructor = false;   
        this.represent = this.accessToString(met.getAccess()) + " ";
        String metName = met.getName();
        if (metName.contains("<init>")) {
            metName = metName.replace("<init>", classFile.getName().getSimpleName());
            isConstructor = true;
        }
        else if (metName.contains("<clinit>")) {
            metName = metName.replace("<clinit>", classFile.getName().getSimpleName());
            isConstructor = true;
        }

        this.represent += metName + "(";

        List<Parameter> params = met.getParameters();

        for (Parameter p : params) {
            
            //Message.showMessage("parameter: " + p.getName() + " " + p.getDescriptor());
            this.represent += p.getName() + ": ";
            this.represent += p.getDeclaration().split(" ")[0];
            
            if (params.get(params.size() - 1) != p)
                this.represent += ", ";
        }

        if (isConstructor) {
            represent += ")";
        }
        else if (met.getTypeSignature() == null) {
            represent += "): " + met.getDeclaration().split(" ")[0];
        }
        else if (met.getTypeSignature().contains(")T")) {
            String typeSignature = met.getTypeSignature();
            //Message.showMessage(met.getTypeSignature());
            String type = new String();
            boolean start = false;

            for (int i = typeSignature.indexOf(")T"); i < typeSignature.length(); i++) {
                // tu konci
                if (typeSignature.charAt(i) == ';') {
                    start = false;
                    continue;
                }
                // tu sa zapise
                if (start == true) {
                    type += typeSignature.charAt(i);
                }
                // tu zacina
                if (typeSignature.charAt(i) == 'T') {
                    start = true;
                    continue;
                }
            }
            
            represent += "): " + type;
        }
        else {
            represent += "): " + met.getDeclaration().split(" ")[0];
        }
/*
        if (met.isStatic()) {
            this.represent += " [static]";
        }
    
        if (met.isAbstract()) {
            this.represent += " [abstract]";
        }*/
    }

    @Override
    public String toString() {
        return this.represent;
    }

    public boolean isConstructor() {
        return isConstructor;
    }

    public void setIsConstructor(boolean isConstructor) {
        this.isConstructor = isConstructor;
    }
    
    
}